/*
 * Servicio.java
 *
 * Created on 28 de junio de 2007, 12:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package servidor;

import java.io.File;
import java.util.Vector;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;


/**
 *
 * @author mfrias
 */

@WebService(name="Servicio", serviceName="Servicio", portName="ServicioPort",targetNamespace = "http://servicio/")
@SOAPBinding(style=SOAPBinding.Style.RPC)
//@XmlRootElement 
public class Servicio {
    
    private Vector ingredientes;
    private Database db;
    
    private Principal ui;
    
    
    
    /** Creates a new instance of Servicio */
    public Servicio(Principal p){
        this.ui = p;
    }
    
    
    
    @WebMethod(operationName="multipleTagSearch")
    public String multipleSearchIngredient( String tagsTxt ) //Vector<String> tags)
    {
        Vector<String> tags = new Vector<String>();
        String [] tmp = tagsTxt.split(";");
        for(int i = 0; i < tmp.length; i++){
            tags.addElement((String)tmp[i]);
        }
        Busqueda b = new Busqueda(this.ui.getIngredientsDirectory());
        String a = b.multipleSearch(tags);
        if(a != null){
            return a;
            
        }
        else{
            ui.log("Error encontrando las claves \" " + tagsTxt + " \". Checkear el directorio de la DB. "  );
            return null;
        }
        
    }
    
    @WebMethod(operationName="simpleTagSearch")
    public String simpleTagSearch( String tag )
    {
        Busqueda b = new Busqueda(this.ui.getIngredientsDirectory());
        String a = b.simpleSearch(tag);
        if(a != null){
            return a;
            
        }
        else{
            ui.log("Error encontrando la clave \" " + tag + "\". Checkear el directorio de la DB. "  );
            return null;
        }
        
        
    }
    
    @WebMethod(operationName="download")
    public String getIngredient(int id){
        
        Busqueda b = new Busqueda(this.ui.getIngredientsDirectory());
        String path = b.getIngredientPathById(id);
        if(path != null){
            File ingredientFile = new File(path);
            ui.log(path);
            FileXML fxml = new FileXML(ingredientFile);
            
            //ui.log("Archivo bajado: " + fxml.toXml().getFirstChild().getFirstChild().getFirstChild().getNodeValue()); //debuggin purposes
            return Util.node2String(fxml.toXml());
        }
        
        return "";
    }
    
    @WebMethod(operationName="upload")
    public boolean uploadXml( String stringFile, String rec ){
        boolean recipe;
        if(rec.equalsIgnoreCase("true")){
            recipe=true;
        }else{
            recipe=false;
        }
        
        FileXML fxml = FileXML.fromXml(stringFile);
        
        if(recipe){
            Recipe reci = Recipe.fromXml( fxml.getXml() );
            reci.save( fxml.getName() );
        }else{
            Ingredient ingre = Ingredient.fromXml( fxml.getXml() );
            ingre.save2( fxml.getName() );
        }
        
        return true;
    }

    public Principal getUi() {
        return ui;
    }

    public void setUi(Principal ui) {
        this.ui = ui;
    }
    
    
}
